package com.xw.floatlib;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Tools {

    public static String getMusicTime(int length) {

        String times = "";
        int minute = length / 60;
        int ss = length % 60;

        if (minute < 1) {
            times = "00:";
        } else {
            if (minute < 10)
                times = "0" + minute + ":";
            else
                times = minute + ":";
        }

        if (ss < 10) {
            times = times + "0" + ss;
        } else {
            times = times + ss;
        }

        return times;
    }

    /**
     * 初始化状态栏，适配沉浸式
     */
    public static void initTitleBarBitMap(Activity activity, View bgView, Bitmap bitmap) {
        //设置沉浸式状态栏
        translucentStatusBar(activity, true);
        //计算出状态栏高度，并设置view留出对应位置
        int height = (int) getStatusBarHeight(activity);
        bgView.setPadding(0, height, 0, 0);

        if (bitmap != null) {
            bgView.setBackground(new BitmapDrawable(activity.getResources(), bitmap));
        }
    }


    /**
     * 获取状态栏高度。
     *
     * @param context context
     * @return 返回获取的状态栏高度
     */
    public static float getStatusBarHeight(Context context) {
        float result = 0;
        try {
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimension(resourceId);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }

    public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
        Window window = activity.getWindow();
        //添加Flag把状态栏设为可绘制模式
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        if (hideStatusBarBackground) {
            //如果为全透明模式，取消设置Window半透明的Flag
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //设置状态栏为透明
            window.setStatusBarColor(Color.TRANSPARENT);
            //设置window的状态栏不可见
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        } else {
            //如果为半透明模式，添加设置Window半透明的Flag
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //设置系统状态栏处于可见状态
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
        //view不根据系统窗口来调整自己的布局
        ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            ViewCompat.requestApplyInsets(mChildView);
        }
    }
}